package city.spring.modules.consumptionItem.service.impl;

import city.spring.modules.consumptionItem.entity.ConsumptionCategoryEntity;
import city.spring.modules.consumptionItem.entity.ConsumptionItemsEntity;
import city.spring.modules.consumptionItem.repository.ConsumptionCategoryRepository;
import city.spring.modules.consumptionItem.service.ConsumptionCategoryService;
import city.spring.modules.consumptionItem.service.ConsumptionItemsService;
import city.spring.modules.consumptionItem.vo.ConsumptionCategoryEntityVO;
import city.spring.utils.TreeDataUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.ListMultimap;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * Service：收费项目类别
 *
 * @author pxm
 * @date 2020-08-05 16:19:31
 */
@CacheConfig(cacheNames = {ConsumptionCategoryServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class ConsumptionCategoryServiceImpl extends ServiceImpl<ConsumptionCategoryRepository, ConsumptionCategoryEntity> implements ConsumptionCategoryService {
    private final ConsumptionCategoryRepository consumptionCategoryRepository;
    private final ConsumptionItemsService consumptionItemsService;

    public ConsumptionCategoryServiceImpl(ConsumptionCategoryRepository consumptionCategoryRepository, ConsumptionItemsService consumptionItemsService) {
        this.consumptionCategoryRepository = consumptionCategoryRepository;
        this.consumptionItemsService = consumptionItemsService;
    }

    @Override
    public void saveConsumptionCategory(ConsumptionCategoryEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateConsumptionCategory(ConsumptionCategoryEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteConsumptionCategory(String primaryKey) {
        removeById(primaryKey);
        consumptionItemsService.lambdaUpdate().eq(ConsumptionItemsEntity::getCategoryId, primaryKey).remove();
    }

    @Override
    public void deleteConsumptionCategory(List<String> primaryKeys) {
        removeByIds(primaryKeys);
        consumptionItemsService.lambdaUpdate().eq(ConsumptionItemsEntity::getCategoryId, primaryKeys).remove();
    }

    @Override
    public List<ConsumptionCategoryEntity> converterToTreeDatasource(@NonNull List<ConsumptionCategoryEntity> rawDatasource, boolean loadParentEntity) {
        ListMultimap<String, ConsumptionCategoryEntity> multimap;
        // 对整体进行排序
        rawDatasource.sort(Comparator.comparing(ConsumptionCategoryEntity::getGmtCreate));
        multimap = TreeDataUtils.handlerTreeDatasource(rawDatasource, "",
                ConsumptionCategoryEntity::getId, ConsumptionCategoryEntity::getPid,
                ConsumptionCategoryEntity::getChildren, ConsumptionCategoryEntity::setChildren);
        if (loadParentEntity) {
            return TreeDataUtils.loadTreeParents(multimap, "", lambdaQuery(),
                    ConsumptionCategoryEntity::getId, ConsumptionCategoryEntity::setChildren);
        }
        return new ArrayList<>(multimap.values());
    }

    @Override
    public List<ConsumptionCategoryEntity> getLittleCategory() {
        return consumptionCategoryRepository.getLittleCategory();
    }

    @Override
    public List<ConsumptionCategoryEntity> getFatherCategory() {
        return consumptionCategoryRepository.getFatherCategory();
    }


    /**
     * 分页获取的消费类别
     *
     * @param page    分页对象
     * @param wrapper 查询对象
     * @return 消费类别
     */
    @Override
    public IPage<ConsumptionCategoryEntityVO> listPage(Page<? extends ConsumptionCategoryEntity> page, Wrapper<? extends ConsumptionCategoryEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }

    @Override
    public ConsumptionCategoryEntity getConsumptionCategoryById(String id) {
        return consumptionCategoryRepository.getConsumptionCategoryById(id);
    }

}
